package com.cloud.pora.mrm.web;

import java.util.List;
import java.util.TimeZone;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;
import org.jboss.seam.log.Log;

import com.cloud.pora.mrm.model.MeetingRoom;
import com.cloud.pora.mrm.model.MeetingRoomBooking;
import com.cloud.pora.mrm.web.common.util.CheckUtil;

@Scope(ScopeType.CONVERSATION)
@Name("meetingRoomBookingListAction")
public class MeetingRoomBookingListAction {
    @Logger
    private Log log;

    @In
    EntityManager entityManager;

    @DataModel("meetingRoomBookings")
    private List<MeetingRoomBooking> meetingRoomBookings;

    @DataModelSelection("meetingRoomBookings")
    MeetingRoomBooking selectedMeetingRoomBooking;

    @In(required = false)
    @Out(required = false)
    MeetingRoomBooking meetingRoomBooking;
    
    


    
/////////////////////////////////
    @DataModel("meetingRooms")
    private List<MeetingRoom> meetingRooms;

    @DataModelSelection("meetingRooms")
    MeetingRoom selectedMeetingRoom;

//    @Out(required = false)
//    MeetingRoom meetingRoom;
//    public List<MeetingRoom> getMeetingRooms() {
//        return meetingRooms;
//    }
////////////////////////////////
    
    
    
    
    
    
    
    

    private boolean initFlag = true;

    public boolean isInitFlag() {
        return initFlag;
    }

    public void setInitFlag(boolean initFlag) {
        this.initFlag = initFlag;
    }

    public void page() {
        initFlag = false;
        this.meetingRoomBookings = entityManager.createQuery(
                "select m from MeetingRoomBooking m").getResultList();

        // ?? test init
        if (this.meetingRoomBooking == null) {
            this.meetingRoomBooking = new MeetingRoomBooking();
            this.meetingRoomBooking.setMeetingRoom(new MeetingRoom());
        }

    }

//    public List<MeetingRoomBooking> getMeetingRoomBookings() {
//        page();
//        return meetingRoomBookings;
//    }

    void buildSearch(String sql, Query query) {

    }

    public String search() {

        /*
         * Query query = entityManager .createQuery(
         * "select mrb from MeetingRoomBooking mrb,MeetingRoom mr where mrb.meetingRoom.mrId=mr.mrId and mr.mrType=:mrType and mr.mrName=:mrName and mrb.meetingStartTime>=:meetingStartTime and mrb.meetingEndTime<=:meetingEndTime"
         * );
         */

        /* create query */
        StringBuffer queryString = new StringBuffer();
        queryString
                .append("select mrb from MeetingRoomBooking mrb,MeetingRoom mr where mrb.meetingRoom.mrId=mr.mrId ");
        if (!CheckUtil.isEmpty(meetingRoomBooking.getMeetingRoom().getMrName())) {
            queryString.append(" and mr.mrName=:mrName");
        }

        if (!CheckUtil.isEmpty(meetingRoomBooking.getMeetingRoom().getMrType())) {
            queryString.append(" and mr.mrType=:mrType");
        }
        if (!CheckUtil.isEmpty(meetingRoomBooking.getMeetingStartTime())) {
            queryString.append(" and mrb.meetingStartTime>=:meetingStartTime");
        }
        if (!CheckUtil.isEmpty(meetingRoomBooking.getMeetingEndTime())) {
            queryString.append(" and mrb.meetingEndTime<=:meetingEndTime");
        }

        /* Executed query */
        Query query = entityManager.createQuery(queryString.toString());

        if (!CheckUtil.isEmpty(meetingRoomBooking.getMeetingRoom().getMrName())) {
            query.setParameter("mrName", meetingRoomBooking.getMeetingRoom()
                    .getMrName());
        }

        if (!CheckUtil.isEmpty(meetingRoomBooking.getMeetingRoom().getMrType())) {
            query.setParameter("mrType", meetingRoomBooking.getMeetingRoom()
                    .getMrType());
        }
        if (!CheckUtil.isEmpty(meetingRoomBooking.getMeetingStartTime())) {
            query.setParameter("meetingStartTime",
                    meetingRoomBooking.getMeetingStartTime());
        }
        if (!CheckUtil.isEmpty(meetingRoomBooking.getMeetingEndTime())) {
            query.setParameter("meetingEndTime",
                    meetingRoomBooking.getMeetingEndTime());
        }

        this.meetingRoomBookings = query.getResultList();
        return "search";

    }

    public String bookingSearch() {

        /*
         * Query query = entityManager .createQuery(
         * "select mrb from MeetingRoomBooking mrb,MeetingRoom mr where mrb.meetingRoom.mrId=mr.mrId and mr.mrType=:mrType and mr.mrName=:mrName and mrb.meetingStartTime NOT BETWEEN :meetingStartTime AND :meetingEndTime and mrb.meetingEndTime NOT BETWEEN :meetingStartTime AND :meetingEndTime"
         * );
         */

        /* create query */
        StringBuffer queryString = new StringBuffer();
        queryString
                .append("select distinct mr.mrId from MeetingRoomBooking mrb,MeetingRoom mr where mrb.meetingRoom.mrId=mr.mrId ");
        if (!CheckUtil.isEmpty(meetingRoomBooking.getMeetingRoom().getMrName())) {
            queryString.append(" and mr.mrName=:mrName");
        }

        if (!CheckUtil.isEmpty(meetingRoomBooking.getMeetingRoom().getMrType())) {
            queryString.append(" and mr.mrType=:mrType");
        }

        queryString.append(" and (");
        queryString.append(" (");
        queryString
                .append(" mrb.meetingStartTime BETWEEN :meetingStartTime AND :meetingEndTime");
        queryString
                .append(" and mrb.meetingEndTime BETWEEN :meetingStartTime AND :meetingEndTime");
        queryString.append(" )");
        queryString.append(" or (");
        queryString
                .append(" :meetingStartTime BETWEEN mrb.meetingStartTime AND mrb.meetingEndTime");
        queryString
                .append(" and :meetingEndTime BETWEEN mrb.meetingStartTime AND mrb.meetingEndTime");

        queryString.append(" and mrb.meetingStartTime > now()");
        queryString.append(" )");
        queryString.append(" )");

        /* create query */
        StringBuffer queryStringMR = new StringBuffer();
        // String
        // sql="select m from MeetingRoom m where m.mrId not in (select mrb.meetingRoom.mrId from MeetingRoomBooking mrb,MeetingRoom mr where mrb.meetingRoom.mrId=mr.mrId and mr.mrType!=:mrType and mr.mrName!=:mrName and mrb.meetingStartTime (NOT BETWEEN :meetingStartTime AND :meetingEndTime) and mrb.meetingEndTime (NOT BETWEEN :meetingStartTime AND :meetingEndTime)";

        queryStringMR.append("select m from MeetingRoom m where m.mrId not in");
        queryStringMR.append("(");
        queryStringMR.append(queryString);
        queryStringMR.append(")");
        if (!CheckUtil.isEmpty(meetingRoomBooking.getMeetingRoom().getMrName())) {
            queryString.append(" and m.mrName=:mrName");
        }

        if (!CheckUtil.isEmpty(meetingRoomBooking.getMeetingRoom().getMrType())) {
            queryString.append(" and m.mrType=:mrType");
        }

        /* Executed query */
        Query query = entityManager.createQuery(queryStringMR.toString());

        if (!CheckUtil.isEmpty(meetingRoomBooking.getMeetingRoom().getMrName())) {
            query.setParameter("mrName", meetingRoomBooking.getMeetingRoom()
                    .getMrName());
        }

        if (!CheckUtil.isEmpty(meetingRoomBooking.getMeetingRoom().getMrType())) {
            query.setParameter("mrType", meetingRoomBooking.getMeetingRoom()
                    .getMrType());
        }
        query.setParameter("meetingStartTime",
                meetingRoomBooking.getMeetingStartTime());
        query.setParameter("meetingEndTime",
                meetingRoomBooking.getMeetingEndTime());

        this.meetingRooms = query.getResultList();

        // this.meetingRooms=entityManager.createQuery("select m from MeetingRoom m").getResultList();

        return "bookingSearch";
    }
    

    public String showDetail() {
        // TODO: search for needing param,get the latest data.
        meetingRoomBooking = selectedMeetingRoomBooking;
        return "meetingRoomBookingDetail";
    }
    
    public String addBookingDetail() {
        //meetingRoomBooking = new MeetingRoomBooking();
        meetingRoomBooking.setMeetingRoom(selectedMeetingRoom);
        return "bookingDetail";
    }

    public long getSize() {
        return meetingRoomBookings == null ? 0 : meetingRoomBookings.size();
    }
    
    public TimeZone getTimeZone() {
//      return TimeZone.getTimeZone("GMT+8");
      return TimeZone.getTimeZone("CCT");
    }

}
